<?php
namespace app\index\controller;
use app\index\business\Common;
use app\index\controller\BaseController;
use Curl\Curl;
use think\Cache;
use think\Config;
use think\Db;
use think\Request;

/**
 * TOP选框的所有选项
 * Class Industry
 * @package app\index\controller
 */
class TopButton extends BaseController
{
    /**
     * 城市
     */
    public function city(){

        $reqData['city_id'] = $this->request->get('city_id',0,'intval');
        $reqData['method'] = $this->request->method();

        /****直辖市****/
        $city_id=$reqData['city_id'];
        if (in_array($reqData['city_id'],[2,25,27,32])){
            $reqData['city_id']=Db::connect(Config::get('db_yd'))->table('yd_city')->where('pid',$reqData['city_id'])->value('id');
        }
        // 业务
        $curl_data = $this->getBusiness('Common')->curl_request_data($reqData['method'],['city_id' => $reqData['city_id']]);
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }

        /****当前产业链的信息***/
        $info = Db::connect(Config::get('db_yd'))->table('yd_city')->where('id', $city_id)->find();
        $this->result['info'] = $info;

        $this->result['status'] = 1;
        $this->result['data'] = $curl_data['data']['data'];
        return json_encode($this->result);
    }

    /**
     * 产业链
     * @return false|string
     */
    public function industryChain()
    {
        $reqData['chain_id'] = (int)$this->request->get('chain_id');
        $reqData['method'] = $this->request->method();
        $username=config('username');
        // 业务
        $curl_data = $this->getBusiness('Common')->curl_request_data($reqData['method'] ,array('chain_id' => $reqData['chain_id'],'username'=>$username));
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }
        /****当前产业链的信息***/
        $info = Db::connect(Config::get('db_yd'))->table('yd_chain')->where('id', $reqData['chain_id'])->find();
        $this->result['info'] = $info;

        $this->result['status'] = 1;
        $this->result['msg'] = 'success';
        $this->result['data'] = $curl_data['data']['data'];
        return json_encode($this->result);
    }

    /**
     * 行业类型
     * @return false|string
     */
    public function industryClassify(){
        $reqData['industry_id'] = $this->request->get('industry_id');
        $reqData['method'] = $this->request->method();
        // 参数校验
        if(!preg_match('/^([0-9][0-9]*){1,10}$/', $reqData['industry_id']))
        {
            $this->result['status'] = -1;
            $this->result['msg'] = '参数错误';
            return json_encode($this->result);
        }
        // 业务
        $curl_data = $this->getBusiness('Common')->curl_request_data($reqData['method'],['industry_id' => $reqData['industry_id']]);
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }

        $this->result['status'] = 1;
        $this->result['msg'] = 'success';
        $this->result['data'] = $curl_data['data']['data'];
        return json_encode($this->result);
    }

    /**
     * 政策形势
     * @return false|string
     */
    public function policytype(){
        $reqData['method'] = $this->request->method();
        $reqData['page'] = $this->request->get('page',0);
        $reqData['limit'] = $this->request->get('limit',10);

        // 业务
        $curl_data = $this->getBusiness('Common')->curl_request_data($reqData['method']);
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }

        // 数据分页返回
        $offset = $reqData['page']*$reqData['limit'];
        $res = array_slice($curl_data['data']['data']['data'],$offset,$reqData['limit']);
        $curl_data['data']['data']['data'] = $res;

        $this->result['status'] = 1;
        $this->result['data'] = $curl_data['data']['data'];
        return json_encode($this->result);
    }

    /**
     * 项目库两个top
     */
    public function projectTop(){
        // 合作方式
        $cooperation = Db::table('cq_cooperation')->select()->toArray();
        $amount_scope = Config::get('amount_scop');
        $result = compact('cooperation','amount_scope');
        $this->result['status'] = 1;
        $this->result['data'] = $result;
        return json_encode($this->result);
    }

    /**
     * 产业链节点下的企业有无
     */
    public function chainMorere()
    {
        $reqData['chain_id'] = $this->request->get('chain_id',null,'intval');
        $reqData['city_id'] = $this->request->get('city_id',null,'intval');
        //$reqData['park_id'] = $this->request->get('city_id',null,'intval');
        $reqData['type']=$this->request->get('type',null,'trim');
        $method = $this->request->method();

        // 业务
        $curl_data = $this->getBusiness('Common')->curl_request_data($method,['type'=>$reqData['type'],'chain_id'=>$reqData['chain_id'],'city_id'=>$reqData['city_id']]);
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }
        $this->result['status'] = 1;
        $this->result['msg'] = 'success';
        $this->result['data'] = $curl_data['data']['data'];
        return json_encode($this->result);
    }

    /**
     * 产业链下拉框
     * @return false|string
     */
    public function chainDropBox()
    {
        /*
        $method = $this->request->method();
        // 业务
        $curl_data = $this->getBusiness('Common')->curl_request_data($method);
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }
        $this->result['status'] = 1;
        $this->result['msg'] = 'success';
        $this->result['data'] = $curl_data['data']['data'];
        return json_encode($this->result);*/



        $reqData['chain_id'] = (int)$this->request->get('chain_id');
        $reqData['method'] = $this->request->method();
        // 业务
        $username=config('username');
        $curl_data = $this->getBusiness('Common')->curl_request_data($reqData['method'] ,array('chain_id' => $reqData['chain_id'],'username'=>$username));
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }
        /****当前产业链的信息***/
        $info = Db::connect(Config::get('db_yd'))->table('yd_chain')->where('id', $reqData['chain_id'])->find();
        $this->result['info'] = $info;

        $this->result['status'] = 1;
        $this->result['msg'] = 'success';
        $this->result['data'] = $curl_data['data']['data'];
        return json_encode($this->result);
    }

    /**
     * 产业链下拉框
     * @return false|string
     */
    public function chainCityMap()
    {
        $reqData['chain_id'] = $this->request->get('chain_id',0,'intval');
        $reqData['city_id'] = $this->request->get('city_id',0,'intval');
        $reqData['type'] = $this->request->get('type',0,'trim');
        $method = $this->request->method();
        // 业务
        if ($reqData['city_id']==0){
            $reqData['city_id']=1;
        }
        $curl_data = $this->getBusiness('Common')->curl_request_data($method,['chain_id'=>$reqData['chain_id'],'type'=>$reqData['type'],'city_id'=>$reqData['city_id']]);
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }
        $this->result['status'] = 1;
        $this->result['msg'] = 'success';
        $this->result['data'] = $curl_data['data']['data'];
        $this->result['info'] = $curl_data['data']['info'];
        return json_encode($this->result);
    }

    /**
     * 重点城市
     */
    public function importantCity()
    {
        $city= [
            ['id'=>2, 'name'=>'北京'],
            ['id'=>25,'name'=>'上海'],
            ['id'=>76, 'name'=>'广州'],
            ['id'=>77, 'name'=>'深圳'],
            ['id'=>32,'name'=>'重庆']
        ];
        return json_encode(array('status'=>1,'msg'=>'success','data'=>$city));
    }

    public function common()
    {
        /****项目跟进状态****/
        $project_status=[
            ['id'=>1,'name'=>'新建'],
            ['id'=>2,'name'=>'在跟'],
            ['id'=>3,'name'=>'签约'],
            ['id'=>4,'name'=>'投产'],
        ];
        $result['project_status']=$project_status;
        return json_encode(array('status'=>1,'msg'=>'success','data'=>$result));
    }

    /**
     * 首页新增产业占比
     */
    public function chainRate()
    {
        $cache=new Cache();
        $result=$cache->get('db_chain_rate');
        if ($result&& 1==2){
            $result=unserialize($result);
        }else{
            $city_id = 77;
            /***该城市企业数***/
            $count=Db::connect(Config::get('db_yd'))->table('yd_companys')->where(['status'=>1,'city_id'=>$city_id])->count();
            /***该城市产业节点下的企业信息***/
            $industry_com=Db::connect(Config::get('db_yd'))
                ->table('yd_companys a')
                ->join('yd_companys_industry b','a.id=b.company_id','left')
                ->group('a.id')
                ->where(['a.status'=>1,'b.status'=>1,'a.city_id'=>$city_id])
                ->column('min(b.industry_id) as min');
            /***每个产业出现的个数***/
            $industry_count=array_count_values($industry_com);
            $industry = Db::connect(Config::get('db_yd'))->table('yd_industry')->where(['status' => 1])->whereIn('id',array_keys($industry_count))->column('industry_name', 'id');

            $new=[];
            foreach ($industry_count as $k=>$val){
                $new[$k]['id']=$k;
                $new[$k]['total']=$val;
                $new[$k]['rate']=bcdiv($val,$count,2)*100;
                if ($new[$k]['rate']<1){
                    $new[$k]['rate']= '1%';
                }else{
                    $new[$k]['rate']=$new[$k]['rate'] .'%';
                }
                $new[$k]['long_name']=$industry[$k];
                $new[$k]['short_name']=mb_substr($industry[$k],0,2);
            }
            $result=array_values($new);
            $result=array_slice($result,0,5);
            array_multisort(array_column($result,'total'),SORT_DESC,$result);
            $cache->set('db_chain_rate', serialize($result),3600);
        }
        return json_encode(array('status'=>1,'msg'=>'success','data'=>$result));
    }

    /**
     * 纠错下拉选项
     */
    public function mistakeType()
    {
        $result=Db::connect(Config::get('db_yd'))->table('yd_mistake_type')->where('status',1)->select();
        return json_encode(array('status'=>1,'msg'=>'success','data'=>$result));
    }

    /**
     * 纠错表单提交
     */
    public function correctMistake(Request $request){
        $data['type_id']=$request->param('type_id','','trim');
        $data['content']=$request->param('content','','trim');
        $data['identification']=$request->param('identification','','trim');
        if (empty($data['identification'])){
            return json_encode(array('status'=>-1,'msg'=>'error','data'=>[]));
        }
        Db::connect(Config::get('db_yd'))->table('yd_mistake')->insert($data);
        return json_encode(array('status'=>1,'msg'=>'success','data'=>[]));
    }
}